<?php
/* coded by e1r0nd 17.01.12 */
/* Placed in public domain      */
class Book {

	private function XML_Header_printout () {
	/*** Вывести заголовок XML ***/
		header("Content-Type: text/xml; charset=utf-8");
		echo'<?xml version="1.0" encoding="utf-8"?>'."\r\n".'<result>'."\r\n";
	}
	
	private function XML_Footer_printout () {
	/*** Вывести подвал XML ***/
		echo'</result>';
	}
	
	private function XML_Book_printout ($r) {
	/*** Вывести описание книги через XML ***/
		echo '<book>'."\r\n";
		echo '<id>'.$r->id.'</id>'."\r\n";
		echo '<position>'.$r->position.'</position>'."\r\n";
		echo '<rate>'.$r->rate.'</rate>'."\r\n";
		echo '<started_at>'.date('d.m.Y',strtotime($r->started_at)).'</started_at>'."\r\n";
		echo '<finished_at>'.date('d.m.Y',strtotime($r->finished_at)).'</finished_at>'."\r\n";
		echo '<title>'.$r->title.'</title>'."\r\n";
		echo '<author>'.$r->author.'</author>'."\r\n";
		echo '<notes>'.$r->notes.'</notes>'."\r\n";
		echo '</book>'."\r\n";
	}
	
	public function XML_Book_view ($list, $uid) {
	/*** Просмотр книг через XML ***/
		$spec = '';
		$add = ' AND `uid` = '.(int) $uid;
		$sort = 'finished_at';
		$asc = 'DESC';
		if ($list == 'favorites')
			$rate = '0'.$add;
		else if ($list == 'now')
			$rate = '6'.$add;
		else if ($list == 'read') {
			$rate = '-1';
			$terms = 'rate > 0 AND rate < 6'.$add;
		}
		else if ($list == 'want') {
			$rate = '7'.$add;
			$sort = 'position';
			$asc = 'ASC';
		}
		else if ($list == 'dont')
			$rate = '8'.$add;
		else $rate = '0'.$add;
		$books = classDB::getInstance()->DB_read('*', 'Books', 'rate', $rate, $terms, $sort, $asc);		
		$this->XML_Header_printout ();
		while ($r = mysql_fetch_object($books)) { //сформировать результат
			$this->XML_Book_printout($r);
		}
		$this->XML_Footer_printout ();
	}
	
	public function Book_delete ($id) {
		if ( (int) $id ) {
// 			echo 'id is '.$id;
			classDB::getInstance()->DB_remove('Books', $id);
		}
	}
	
	public function Book_edit ($id, $title, $author, $notes, $rate, $started_at, $finished_at) {
		$id = (int) $id;
		$title = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$title);
		$title = substr($title, 0, 99);
		$author = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$author);
		$author = substr($author, 0, 50);
		$notes = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$notes);
		$notes = substr($notes, 0, 250);
		$rate = (int) $rate;
		$rate = ($rate > 8) ? 0 : $rate;
		$started_at = preg_replace ("/[^0-9.\s]/","",$started_at);
		$started_at = date("Y.m.d", strtotime(substr($started_at, 0, 10)));
		$finished_at = preg_replace ("/[^0-9.\s]/","",$finished_at);
		$finished_at = date("Y.m.d", strtotime(substr($finished_at, 0, 10)));
	
		classDB::getInstance()->DB_update('Books', "`title` = '$title', `author` = '$author', `notes` = '$notes', `rate` = '$rate', `started_at` = '$started_at', `finished_at` = '$finished_at'", $id);
	
		$books = classDB::getInstance()->DB_read('rate', 'Books', 'id', $id);
		$r = mysql_fetch_object($books);
		echo $r->rate,$id;
	}
	
	public function Book_add ($uid, $title, $author, $notes, $rate, $started_at, $finished_at) {
		$uid = (int) $uid;
		$title = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$title);
		$title = substr($title, 0, 99);
		$author = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$author);
		$author = substr($author, 0, 50);
		$notes = preg_replace ("/[^0-9a-zA-Zа-яА-ЯёЁыЫцушщрэчтьюхъф .,-:()\s]/","",$notes);
		$notes = substr($notes, 0, 250);
		$rate = (int) $rate;
		$rate = ($rate > 8) ? 0 : $rate;
		$started_at = preg_replace ("/[^0-9.\s]/","",$started_at);
		$started_at = date("Y.m.d", strtotime(substr($started_at, 0, 10)));
		$finished_at = preg_replace ("/[^0-9.\s]/","",$finished_at);
		$finished_at = date("Y.m.d", strtotime(substr($finished_at, 0, 10)));

		classDB::getInstance()->DB_insert('Books', "`uid`, `id`, `position`, `title`, `author`, `notes`, `rate`, `started_at`, `finished_at`", "'$uid', NULL, '', '$title', '$author', '$notes', '$rate', '$started_at', '$finished_at'");
		
		$books = classDB::getInstance()->DB_read('id,rate', 'Books', '1', '1', '', 'id', 'DESC LIMIT 1');
		$r = mysql_fetch_object($books);
		echo $r->rate,$r->id;
	}
	
	public function Book_restore_default($uid) {
		classDB::getInstance()->DB_restore_default($uid);
	}

	public function Book_newsort($newsort) {
		$temp = explode('_', $newsort);
		foreach ($temp as $value) {
			$temp1 = explode('-', $value);
			classDB::getInstance()->DB_update('Books', "`position` = '$temp1[1]'", $temp1[0]);
		}
	}
	
	public function Book_stats($uid) {
		$uid = (int) $uid;
		//сколько прочитано в этом месяце
		//SELECT * FROM `Books` WHERE YEAR(finished_at) = YEAR(CURDATE()) AND MONTH(finished_at) = MONTH(CURDATE())

		$books = classDB::getInstance()->DB_count("YEAR(finished_at) = YEAR(CURDATE()) AND MONTH(finished_at) = MONTH(CURDATE()) AND `uid`=$uid");
		$r = mysql_fetch_array($books);
		$data['current_month']=$r[0];

		//сколько прочитано в прошлом месяце
		//SELECT * FROM `Books` WHERE YEAR(finished_at) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(finished_at) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
		$books = classDB::getInstance()->DB_count("YEAR(finished_at) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(finished_at) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) AND `uid`=$uid");
		$r = mysql_fetch_array($books);
		$data['last_month']=$r[0];
		
		//сколько прочитано в этом году
		//SELECT * FROM `Books` WHERE YEAR(finished_at) = YEAR(CURDATE())
		$books = classDB::getInstance()->DB_count("YEAR(finished_at) = YEAR(CURDATE()) AND `uid`=$uid");
		$r = mysql_fetch_array($books);
		$data['current_year']=$r[0];
		
		//сколько прочитано в прошлом году
		//SELECT * FROM `Books` WHERE YEAR(finished_at) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR)
		$books = classDB::getInstance()->DB_count("YEAR(finished_at) = YEAR(CURRENT_DATE - INTERVAL 1 YEAR) AND `uid`=$uid");
		$r = mysql_fetch_array($books);
		$data['last_year']=$r[0];
		
		//сколько книг еще читается
		//SELECT * FROM `Books` WHERE YEAR(finished_at) = YEAR('0000-00-00')
		$books = classDB::getInstance()->DB_count("YEAR(finished_at) = YEAR('0000-00-00') AND `uid`=$uid");
		$r = mysql_fetch_array($books);
		$data['read_now']=$r[0];
		
		//сколько книг отложено больше месяца назад
		//SELECT * FROM `Books` WHERE Date(started_at)<Date(CURRENT_DATE - INTERVAL 1 MONTH) AND Date(started_at) != Date('0000-00-00') AND Date(finished_at) = Date('0000-00-00') 
		$books = classDB::getInstance()->DB_count("Date(started_at)<Date(CURRENT_DATE - INTERVAL 1 MONTH) AND Date(started_at) != Date('0000-00-00') AND Date(finished_at) = Date('0000-00-00') AND `uid`=$uid");
		$r = mysql_fetch_array($books);
		$data['delayed']=$r[0];
		
		//всего прочитано
		$books = classDB::getInstance()->DB_count(" `uid`=$uid");
		$r = mysql_fetch_array($books);
		$data['all']=$r[0];
		
		echo json_encode($data);
	}
	
	public function Book_graph($uid) {
		$uid = (int) $uid;
		for ($month=1; $month<=12; $month++) {
			$books = classDB::getInstance()->DB_count("YEAR(finished_at) = YEAR(CURDATE()) AND MONTH(finished_at) = '$month' AND `uid`=$uid");
			$r = mysql_fetch_array($books);
			$data[$month.'_finished']=$r[0];
			$books = classDB::getInstance()->DB_count("YEAR(started_at) = YEAR(CURDATE()) AND MONTH(started_at) = '$month' AND YEAR(finished_at) = YEAR('0000-00-00') AND `uid`=$uid");
			$r = mysql_fetch_array($books);
			$data[$month.'_started']=$r[0];
			$books = classDB::getInstance()->DB_count("YEAR(finished_at) = YEAR(CURDATE()) AND MONTH(finished_at) = '$month' AND YEAR(started_at) = YEAR(CURDATE()) AND MONTH(started_at) = '$month' AND `uid`=$uid");
			$r = mysql_fetch_array($books);
			$data[$month.'_read']=$r[0];
		}
		echo json_encode($data);
	}
}

?>